﻿#include "OperatorsMeshGen.h"

#include "FITK_Component/FITKGmshExeDriver/FITKGmshExecProgramDriver.h"
#include "FITK_Component/FITKGmshExeDriver/FITKGmshExecProgramInputInfo.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKGeoInterfaceFactory.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoModelExport.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppSettings.h"
#include "FITK_Kernel/FITKAppFramework/FITKProgramTaskManager.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKAppFramework/FITKComponents.h"
#include "FITK_Component/FITKGmshMshIO/FITKGmshMshIOInterface.h"

#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsFEModel.h"
#include "FITK_Component/FITKAbaqusData/FITKAbaqusPart.h"
#include "FITK_Interface/FITKInterfaceModel/FITKComponentManager.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMeshPartVTK.h"
#include "OperatorsInterface/GraphEventOperator.h"

#include "FITK_Interface/FITKInterfaceMeshGen/FITKMeshGenInterface.h"
#include "FITK_Interface/FITKInterfaceMeshGen/FITKGlobalMeshSizeInfo.h"
#include "FITK_Interface/FITKInterfaceMeshGen/FITKGlobalMeshGenerateAlgorithmInfo.h"

#include <QDir>

namespace ModelOper
{
    OperatorsMeshGen::OperatorsMeshGen()
    {

    }
    OperatorsMeshGen::~OperatorsMeshGen()
    {
    }
    bool OperatorsMeshGen::execGUI()
    {
        return true;
    }
    bool OperatorsMeshGen::execProfession()
    {
        /*Interface::FITKMeshGenInterface* meshGen = Interface::FITKMeshGenInterface::getInstance();
        if (!meshGen)return false;
        Interface::FITKGlobalMeshSizeInfo* sizeInfo = meshGen->getGlobalMeshSizeInfo();
        if (!sizeInfo)return false;
        sizeInfo->setMaxSize(1);
        sizeInfo->setMinSize(1);*/

        AppFrame::FITKAppSettings* settings = FITKAPP->getAppSettings();
        if (!settings) return false;
        //获取工作目录
        QString workDir = settings->getWorkingDir();
        QDir dir(workDir);
        if (!dir.exists()) return false;
        //写出几何文件
        QString shapeFile = workDir + "/Gmsh/geometryFile.step";
        QFile::remove(shapeFile);
        //获取全局数据管理器
        AppFrame::FITKGlobalData* globalData = FITKAPP->getGlobalData();
        if (!globalData)return false;
        //获取几何接口工厂
        Interface::FITKInterfaceGeometryFactory* fac = Interface::FITKInterfaceGeometryFactory::getInstance();
        if (fac == nullptr) return false;
        Interface::FITKAbsGeoCommand* commGeo = fac->createCommand(Interface::FITKGeoEnum::FGTExport);
        if (!commGeo) return false;
        Interface::FITKAbsGeoModelExport* geoExport = dynamic_cast<Interface::FITKAbsGeoModelExport*>(commGeo);
        if (!geoExport)
        {
            delete commGeo;
            return false;
        }
        geoExport->setFileName(shapeFile);
        if (!geoExport->update()) return false;

        QString geoScriptFile = workDir + "/Gmsh/gmsh.geo";
        QString meshFile = workDir + "/Gmsh/meshFile.msh";
        meshFile.split("");
        GmshExe::FITKGmshExecProgramInputInfo* inputInfo = new GmshExe::FITKGmshExecProgramInputInfo;
        inputInfo->setGeoScriptFile(geoScriptFile);
        inputInfo->setMeshFile("msh", meshFile);
        AppFrame::FITKProgramTaskManeger* programTaskMgr = FITKAPP->getProgramTaskManager();
        GmshExe::FITKGmshExecProgramDriver* gmshExe = dynamic_cast<GmshExe::FITKGmshExecProgramDriver*>(programTaskMgr->createProgram(1, "FITKGmshExecProgramDriver", inputInfo));
        if (!gmshExe)return false;
        if (!gmshExe->isExistExeProgram())
        {
            delete gmshExe;
            return false;
        }
        gmshExe->writeConfigFile(shapeFile, geoScriptFile, meshFile);
        
        
        //gmshExe->setExecProgram(workDir + "/Gmsh/gmsh.exe");
        gmshExe->setWorkingDirectory(workDir + "/Gmsh/");
        gmshExe->start();

        this->setArgs("MeshFile", meshFile);
        connect(gmshExe, SIGNAL(sig_Finish()), this, SLOT(gmshMshImport()));
        return true;
    }

    void OperatorsMeshGen::gmshMshImport()
    {
        QString meshFile;
        if (!this->argValue<QString>("MeshFile", meshFile)) return;
        //获取读取组件
        Gmsh::FITKGmshMshIOInterface* fitkAbaIO = new Gmsh::FITKGmshMshIOInterface;
        if (fitkAbaIO == nullptr) return;
        //获取FE模型
        Acoustics::FITKAcousticsDiscModel* model =
            FITKAPP->getGlobalData()->getMeshData<Acoustics::FITKAcousticsDiscModel>();
        if (model == nullptr) return;
        Acoustics::FITKAcousticsFEModel* feModel = model->getFEModel();
        if (!feModel) return;
        AbaqusData::FITKAbaqusPartManager* partMgr = feModel->getPartManager();
        if (!partMgr) return;
        AbaqusData::FITKAbaqusPart* partData = new AbaqusData::FITKAbaqusPart;
        partMgr->appendDataObj(partData);
        //关联信号，后续处理结束的事件
        connect(fitkAbaIO, SIGNAL(ioThreadFinishedSig()), this, SLOT(ioThreadFinishedSlot()));
        //参数设置
        fitkAbaIO->setDataObject("ComponentManager", dynamic_cast<Core::FITKAbstractDataObject*>(partData->getComponentManager()));
        fitkAbaIO->setDataObject("MeshData", dynamic_cast<Core::FITKAbstractDataObject*>(partData->getMesh()));
        fitkAbaIO->setFileName(meshFile);
        //执行读取，1-读取 2- 写出
        fitkAbaIO->exec(1);
        this->setArgs("PartObjID", partData->getDataObjectID());
    }

    void OperatorsMeshGen::ioThreadFinishedSlot()
    {
        int dataObjID = 0;
        if (!this->argValue<int>("PartObjID", dataObjID)) return;
        //获取可视对象数据
        EventOper::GraphEventOperator* graphOper = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
        if (!graphOper) return;
        graphOper->updateGraph(dataObjID);
    }
}

